package org.bite.lesson02;


/**
 * 分别用串行和并行做两轮10亿次累加操作
 *
 * @Author kiku-wa
 */
public class Demo_201 {
    //大数可以使用分隔符_
    private static long count = 50000;
    public static void main(String[] args) throws InterruptedException{
        //串行
        serial();
        //并行
        concurrency();
    }
    private static void concurrency() throws InterruptedException{
        //记录开始时间
        long begin = System.currentTimeMillis();
        //创建两个线程，各自累加
        Thread t1 = new Thread(()->{
                long a = 0l;
                for(int i = 0; i < count; i++){
                    a++;
                }
        });
        t1.start();//启动线程
        Thread t2 = new Thread(()->{
            long b = 0l;
            for(int i = 0; i < count; i++){
                b++;
            }
        });
        t2.start();//启动线程

        //等待t1、t2执行完成
        t1.join();
        t2.join();

        long end = System.currentTimeMillis();

        System.out.println("并行执行时间："+ (end - begin));
        
    }
    private static void serial(){
        //记录开始时间
        long begin = System.currentTimeMillis();
        long a = 0l;
        for(int i = 0; i < count; i++){
            a++;
        }
        long b = 0l;
        for(int i = 0; i < count; i++){
            b++;
        }
        //记录结束时间
        long end = System.currentTimeMillis();
        System.out.println("串行执行时间：" + (end - begin));
    }
}
